package com.itheima.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.wms.controller.dto.LoginRequest;
import com.itheima.wms.controller.vo.UserInfoVo;
import com.itheima.wms.mapper.UserMapper;
import com.itheima.wms.service.LoginService;
import com.itheima.wms.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Administrator
 */
@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {

    private final UserMapper userMapper;

    @Override
    public UserInfoVo login(LoginRequest request) {

        UserInfoVo userInfoVo = userMapper.queryUsernameAndPassword(request);

        if (userInfoVo == null) {
            return null;
        }

        Map<String, Object> claims = BeanUtil.beanToMap(userInfoVo);

        String jwt = JwtUtil.createJwt(claims);

        userInfoVo.setToken(jwt);

        return userInfoVo;
    }
}
